package com.atguigu.yygh.user.controller.front;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/1/4 - 16:10
 * @Author: Hyoung
 * @Description:
 */

@Api(tags = "就诊人接口")
@RestController
@RequestMapping("/front/user/patient")
@Slf4j
public class FrontPatientController {
    @Autowired
    private PatientService patientService;

    @ApiOperation("添加就诊人")
    @PostMapping("/auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        log.info("savePatient()" + patient);
        return R.ok().message("添加成功");
    }

    @ApiOperation("修改就诊人")
    @PutMapping("/auth/update")
    public R updatePatient(@RequestBody Patient patient, HttpServletRequest request) {
        AuthContextHolder.checkAuth(request);
        patientService.updateById(patient);
        //安全校验
        return R.ok().message("修改就诊人成功");
    }

    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("/auth/get/{id}")
    public R getPatient(@PathVariable Long id, HttpServletRequest request) {
        AuthContextHolder.checkAuth(request);
        Patient patient = patientService.getPatientById(id);
        return R.ok().data("patient", patient);
    }

    @ApiOperation("获取就诊人列表")
    @GetMapping("/auth/findAll")
    public R findAll(HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllByUserId(userId);
        return R.ok().data("list", list);
    }

    @ApiOperation("删除就诊人")
    @DeleteMapping("/auth/remove/{id}")
    public R removePatient(@PathVariable Long id, HttpServletRequest request) {
        //安全校验
        AuthContextHolder.checkAuth(request);
        patientService.removeById(id);
        return R.ok();
    }
}
